<!-- <template>
	<view class="box">
		<view class="contText">

			<view class="flex alignC" style="margin: 0 0 60rpx 20rpx;">
				<u-input v-model="title" type="textarea" placeholder="添加标题" showWordLimit="true" maxlength="20"
					border="none" @change="change"></u-input>
				<text style="color: #ccc;margin-right: 10rpx;">
					{{ showCount }}
				</text>
			</view>
			<u-textarea v-model="content" placeholder="在这里输入文章内容..." border="none" height="300rpx"></u-textarea>
			<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="4"
				uploadIcon="plus"></u-upload>
		</view>



		<view class="foot_box">
			<view class="large_btn btn_primary" @click="postMes" style="background: #00B4C7;font-size: 34rpx;">
				发布帖子
			</view>
		</view>
	</view>
</template>

<script>
	import {
		addOrUpdateApi,
		addViewByIdApi,
		deleteByIdApi,
		queryListByPageApi,
		queryCategoryListApi
	} from '../../common/API/articleApi.js'
	export default {
		data() {
			return {
				title: '',
				content: '',
				showCount: '20',
				fileList: [{
					url: 'https://cdn.uviewui.com/uview/swiper/1.jpg',
				}],
				categoryId: '',
				categoryName: ''
			}
		},
		methods: {
			postMes() {
				this.addOrUpdate()
			},
			change(e) {
				this.showCount = e.length
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			async addOrUpdate() {
				const res = await addOrUpdateApi({
					articleDigest: this.title,
					categoryId: this.categoryId,
					categoryName: this.categoryName,
					content: this.content,
					title: this.title,
					type: '2' // 文章：type 3-继续教育2-经验交流
				})
				console.log('新增返回', JSON.stringify(res))
				if(res.status == 200){
					uni.showToast({
						title:"发布成功"
					}, 1500)
					setTimeout(()=>{
						uni.navigateBack()
					},1500)
					
				}
			},
			async queryCategoryList(){
				const res = await queryCategoryListApi()
				console.log('分类', JSON.stringify(res))
				if(res.status == 200 && res.data.records.length){
					this.categoryId = res.data.records[0].id
					this.categoryName = res.data.records[0].name
				}
				
			},
			
		},
		onLoad() {
			this.queryCategoryList()
		}
	
	}
</script>
<style>
	page {
		background: #fff;
	}
</style>
<style scoped lang="scss">
	.contText {
		padding: 20rpx;

		/deep/ .u-input {}
	}
</style> -->